package cc.wsyw126.java.nowcoder.sword_offer;

/**
 * Created by Administrator on 2017/4/14.
 * 输入一棵二叉树，判断该二叉树是否是平衡二叉树。
 */
public class IsBalanced_Solution {
    public boolean IsBalanced_Solution(TreeNode root) {
        if (root == null) return true;
        int leftCount = TreeDepth(root.left);
        int rightCount = TreeDepth(root.right);
        int diff = rightCount - leftCount;
        if (diff > 1 || diff < -1) {
            return false;
        }
        return IsBalanced_Solution(root.right) && IsBalanced_Solution(root.left);
    }
    public int TreeDepth(TreeNode root) {
        if (root == null) return 0;
        int leftCount = TreeDepth(root.left);
        int rightCount = TreeDepth(root.right);
        return leftCount>rightCount?leftCount+1:rightCount+1;
    }
}
